package com.ruoyi.report_lqhhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlDwddjlbBsDetail;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlDwddjlbBsDetailVo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlDwddjlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqhhl.domain.bo.ReportLqhhlDwddjlbBsInfoBo;
import com.ruoyi.report_lqhhl.domain.vo.ReportLqhhlDwddjlbBsInfoVo;
import com.ruoyi.report_lqhhl.domain.ReportLqhhlDwddjlbBsInfo;
import com.ruoyi.report_lqhhl.mapper.ReportLqhhlDwddjlbBsInfoMapper;
import com.ruoyi.report_lqhhl.service.IReportLqhhlDwddjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青混合料动稳定度试验检测记录Service业务层处理
 *
 * @author llb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
public class ReportLqhhlDwddjlbBsInfoServiceImpl implements IReportLqhhlDwddjlbBsInfoService {

    private final ReportLqhhlDwddjlbBsInfoMapper baseMapper;
    private final ReportLqhhlDwddjlbBsDetailMapper detailMapper;

    /**
     * 查询沥青混合料动稳定度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportLqhhlDwddjlbBsInfoVo queryById(Long excelId){
        ReportLqhhlDwddjlbBsInfoVo reportLqhhlDwddjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportLqhhlDwddjlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<ReportLqhhlDwddjlbBsDetail>().eq(ReportLqhhlDwddjlbBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            reportLqhhlDwddjlbBsInfoVo.setDetailList(detailList);
        }
        return reportLqhhlDwddjlbBsInfoVo;
    }

    /**
     * 查询沥青混合料动稳定度试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportLqhhlDwddjlbBsInfoVo> queryPageList(ReportLqhhlDwddjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqhhlDwddjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqhhlDwddjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青混合料动稳定度试验检测记录列表
     */
    @Override
    public List<ReportLqhhlDwddjlbBsInfoVo> queryList(ReportLqhhlDwddjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqhhlDwddjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqhhlDwddjlbBsInfo> buildQueryWrapper(ReportLqhhlDwddjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqhhlDwddjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqhhlDwddjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqhhlDwddjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqhhlDwddjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqhhlDwddjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqhhlDwddjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqhhlDwddjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqhhlDwddjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqhhlDwddjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqhhlDwddjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqhhlDwddjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLqhhlDwddjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqhhlDwddjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqhhlDwddjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqhhlDwddjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqhhlDwddjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqhhlDwddjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqhhlDwddjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getShiyljdyq() != null, ReportLqhhlDwddjlbBsInfo::getShiyljdyq, bo.getShiyljdyq());
        lqw.eq(StringUtils.isNotBlank(bo.getShijzzff()), ReportLqhhlDwddjlbBsInfo::getShijzzff, bo.getShijzzff());
        lqw.eq(bo.getKongxl() != null, ReportLqhhlDwddjlbBsInfo::getKongxl, bo.getKongxl());
        lqw.eq(bo.getSywd() != null, ReportLqhhlDwddjlbBsInfo::getSywd, bo.getSywd());
        lqw.eq(bo.getShijmd() != null, ReportLqhhlDwddjlbBsInfo::getShijmd, bo.getShijmd());
        lqw.eq(bo.getBianyxs() != null, ReportLqhhlDwddjlbBsInfo::getBianyxs, bo.getBianyxs());
        lqw.eq(bo.getPingjdwdd() != null, ReportLqhhlDwddjlbBsInfo::getPingjdwdd, bo.getPingjdwdd());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqhhlDwddjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportLqhhlDwddjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportLqhhlDwddjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportLqhhlDwddjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), ReportLqhhlDwddjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqhhlDwddjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqhhlDwddjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqhhlDwddjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增沥青混合料动稳定度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLqhhlDwddjlbBsInfoBo bo) {
        ReportLqhhlDwddjlbBsInfo add = BeanUtil.toBean(bo, ReportLqhhlDwddjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportLqhhlDwddjlbBsDetail> details = new ArrayList<>(3);
            for (int i=1;i<=3;i++ ){
                details.add(new ReportLqhhlDwddjlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改沥青混合料动稳定度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLqhhlDwddjlbBsInfoBo bo) {
        ReportLqhhlDwddjlbBsInfo update = BeanUtil.toBean(bo, ReportLqhhlDwddjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportLqhhlDwddjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqhhlDwddjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青混合料动稳定度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
